package net.googlecode.vasetskiy.task4;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.StringTokenizer;


/**
 * Created by IntelliJ IDEA.
 * User: Vasetskiy Vlad
 * Date: 05.04.11
 * Time: 14:08
 * To change this template use File | Settings | File Templates.
 */

/**
 * Class for task4. Realize methods of transformation of string.
 *
 * @author Vasetskiy Vlad
 */

public class NormalizString {

    /*
     *  Constant string, that is operand in methods of class.
     */
   private static final String inputstr=
          "heLlO \t woRlD \n,A,a, i lOvE  coOkiEs \t\t\t\t ";
   private static Integer DATE = -20;
   private static final String strDelimiter=" \t\n,";

   /*
    *  procedure to remove extra separators
    *
    * @param str input string
    * @return temp changed string
    */


   public  String normalDelimiter(String str){
        StringTokenizer stringTokenizer = new StringTokenizer(str, strDelimiter, false);
        String temp = new String();
       while (stringTokenizer.hasMoreTokens()){
          temp += stringTokenizer.nextToken()+str.charAt(13);
       }


      return temp;
   }
    /*
    *  procedure for correcting register
    *
    * @param str input string
    * @return temp changed string
    */

   public void normalRegister(final StringBuffer str){
       str.setCharAt(0,Character.toUpperCase(str.charAt(0)));
           for (int i = 1; i < str.length(); i++) {
               str.setCharAt(i,Character.toLowerCase(str.charAt(i)));
           }
   }
   /*
   * This procedure  logging  reduction to normal form of a string
   *
   */
    public void loggerNormalForm(){
    StringBuffer normstr = new StringBuffer(normalDelimiter(inputstr));
    normalRegister(normstr);
     Logger log = new Logger();
     log.logger("normalized form string",normstr.toString());
 }

    /*
     * procedure change the registry  first  letters  of each word
     *
     * @param str input string
     * @return strBuf.toString() changed string
     */

    public String upperCaseWords(String str){
     StringTokenizer strToken = new StringTokenizer(str, strDelimiter, true);
     StringBuffer strBuf = new StringBuffer();
      while (strToken.hasMoreTokens()){
       String tempToken = strToken.nextToken();
       StringBuffer strBufTemp = new StringBuffer(tempToken);
         if (strBufTemp.toString().contains(" \t\n,")==false) {strBufTemp.setCharAt(0, Character.toUpperCase(strBufTemp.charAt(0)));} else {
          strBufTemp.setCharAt(0, Character.toUpperCase(strBufTemp.charAt(0))); }

          strBuf.append(strBufTemp);
      }

     return strBuf.toString();
    }

   /*
   * This procedure logging upper case words
   */

   private void loggerUpperCaseWords(){
   String upperCase = upperCaseWords(inputstr);
     Logger log = new Logger();
     log.logger("upper case words",upperCase);

   }

   /*
    * process of turning words per line
    *
    * @param str input string
    * @return strBuf.toString() changed string
    */

    public String reverseLetter(String str){
      StringTokenizer strToken = new StringTokenizer(str, strDelimiter, false);
      StringBuffer strBuf = new StringBuffer();
       while (strToken.hasMoreTokens()){
        String tempToken = strToken.nextToken();
        StringBuffer strBufTemp = new StringBuffer(tempToken);
        strBufTemp.reverse();
        strBuf.append(strBufTemp);
        }
      return strBuf.toString();


    }

    /*
     * This procedure logging reverse letter in the line
     */

   private void loggerReverseLeter(){
    String reversStr = reverseLetter(inputstr);
     Logger log = new Logger();
      log.logger("reverse letter in the word",reversStr);
   }

   /*
    * the procedure for replacing the letter 'a' and 'A' on '!'
    *
    * @param str input string
    * @return strBuf.toString() changed string
    */

   public String changeSymbol(String str){
     StringBuffer strBuf = new StringBuffer(str);
      for (int i = 0;i<strBuf.length();i++){
         if ((strBuf.charAt(i)=='a') || (strBuf.charAt(i)=='A') ){
          strBuf.setCharAt(i,'!');
         }
      }
    return strBuf.toString();
   }

    /*
     * This procedure logging changed symbol in the line
     */

   public void loggerChangeSymbol(){
      String changeStr = changeSymbol(inputstr);
       Logger log = new Logger();
        log.logger("change symbol 'A' and 'a' on '!' in the word ", changeStr);
   }
  /*
   *  procedure for adding a date
   *
   *  @param str input string
   *  @return strAdd changed string
   */


public String addDate(String str) {
     String strAdd = str;
        final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm");
        final Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, DATE);
        return strAdd + sdf.format(cal.getTime());
    }

    /*
     * This procedure logging add date in the line
     */

     public void loggerAddDate(){
       String addStr = addDate(inputstr);
        Logger log = new Logger();
        log.logger("add the date to end of line ", addStr);
      }

  /*
   * Main procedure to start
   * output  in  the console of all operations on the  line  and their results
   */

  public static void main(String[] args){
    net.googlecode.vasetskiy.task4.NormalizString ns = new net.googlecode.vasetskiy.task4.NormalizString();
     ns.loggerNormalForm();
     ns.loggerUpperCaseWords();
     ns.loggerReverseLeter();
     ns.loggerChangeSymbol();
     ns.loggerAddDate();
  }


}
